<?php

namespace app\controllers;

use app\models\Matrix;
use Yii;

class MatrixController extends \yii\rest\Controller
{
    public function behaviors() {
        return [
            [
                'class' => \yii\filters\ContentNegotiator::className(),
                'only' => ['multiplication'],
                'formats' => [
                    'application/json' => \yii\web\Response::FORMAT_JSON,
                ],
            ],
        ];
    }

    public function actions()
    {
        $actions = parent::actions();
        unset($actions["multiplication"]);
        return $actions;
    }

    public function actionMultiplication() {
        set_time_limit(1000);
        ini_set('memory_limit', '512M');

        $request = Yii::$app->request->post();

        $matrix1 = new Matrix();
        $matrix2 = new Matrix();

        $matrix1->initWithMatrix($request["first"]);
        $matrix2->initWithMatrix($request["second"]);

        return Matrix::multiply($matrix1, $matrix2)->getData();
    }
}